<template>
  <div class="login-container">
    <el-form
      ref="loginFromRef"
      :model="loginFrom"
      :rules="loginRules"
      class="demo-ruleForm"
      auto-complete="on"
      label-position="left"
    >
      <div class="title-container">
        <h3 class="title">学院后台管理系统</h3>
      </div>
      <el-form-item prop="username">
        <span class="icon-container">
          <i class="el-icon-user-solid" />
        </span>
        <el-input
          ref="username"
          v-model="loginFrom.username"
          placeholder="用户名"
          name="username"
          type="text"
          tabindex="1"
          auto-complete="on"
          clearable
        />
      </el-form-item>
      <el-form-item prop="password">
        <span class="icon-container">
          <svg-icon class-name="password-icon" icon-class="password" />
        </span>
        <el-input
          ref="password"
          v-model="loginFrom.password"
          placeholder="请输入密码"
          name="password"
          type="password"
          tabindex="2"
          auto-complete="on"
          show-password
          @keyup.enter.native.prevent="handleLogin"
        />
      </el-form-item>
      <el-checkbox
        v-model="checked"
        style="color: #a0a0a0; margin: 0 0 20px 0"
        @change="clearCookie"
      >记住账号和密码</el-checkbox>
      <a href="">随便看看</a>
      <!-- 登录按钮 -->
      <el-button
        :loading="loading"
        type="primary"
        style="width: 100%; margin-top: 20px"
        @click.native.prevent="handleLogin"
      >登录</el-button>
    </el-form>
  </div>
</template>
<script>
import { validUsername } from '@/utils/validate'
import { Encrypt, Decrypt } from '@/utils/auth'
export default {
  name: 'Loain',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error('请输入正确的用户名'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('密码不能少于6位'))
      } else {
        callback()
      }
    }
    return {
      checked: true,
      loginFrom: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [
          { required: true, trigger: 'blur', validator: validateUsername }
        ],
        password: [
          { required: true, trigger: 'blur', validator: validatePassword }
        ]
      },
      loading: false
    }
  },
  mounted() {
    this.account() // 获取cookie的方法
  },
  methods: {
    account() {
      if (document.cookie.length >= 0) {
        console.log(this.getCookie('username'))
        this.loginFrom.username = this.getCookie('username')
        this.loginFrom.password = Decrypt(this.getCookie('password'))
      }
    },
    setCookie(c_name, c_pwd, exdate) {
      // 账号，密码，过期的天数
      var currentDate = new Date()
      console.log(c_name, c_pwd)
      currentDate.setTime(currentDate.getTime() + 24 * 60 * 60 * 1000 * exdate) // 保存的天数
      document.cookie =
        'username=' + c_name + ';path=/;expires=' + currentDate.toLocaleString()
      document.cookie =
        'password=' + c_pwd + ';path=/;expires=' + currentDate.toLocaleString()
    },
    getCookie(name) {
      var arr = document.cookie.split(';')
      for (var i = 0; i < arr.length; i++) {
        var arr2 = arr[i].split('=')
        if (arr2[0].trim() === name) {
          return arr2[1]
        }
      }
    },
    clearCookie() {
      console.log(this.checked, this.loginFrom)
      if (!this.checked) this.setCookie('', '', 1) // 清除cookie
    },
    handleLogin() {
      this.$refs.loginFromRef.validate((valid) => {
        if (valid) {
          this.loading = true
          var _this = this
          if (_this.checked === true) {
            // 存入cookie
            this.setCookie(_this.loginFrom.username, Encrypt(this.loginFrom.password), 7) // 保存7天
          } else {
            _this.clearCookie()
          }
          this.$store
            .dispatch('user/login', this.loginFrom)
            .then(() => {
              this.$message.success('恭喜您！登录成功。')
              this.$router.push({ path: '/' }, () => {})
            })
            .catch((err) => {
              console.log('登录失败', err)
            })
        }
      })
    }
  }
}
</script>
<style lang="scss">
$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      // background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: #000;
      background-color: #fff;
      height: 47px;
      caret-color: #000;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px #fff inset !important;
        -webkit-text-fill-color: #283443 !important;
      }
    }
  }
  .el-form-item {
    // border: 1px solid #dad8d8;
    // background-color: #5b6f94;
    background-color: #fff;

    border-radius: 5px;
    color: #454545;
  }
  a {
    font-size: 12px;
    color: #1890ff;
    text-decoration: underline;
    float: right;
  }
}
</style>
<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  min-height: 100%;
  overflow: hidden;
  // background-color: $bg;
  width: 100%;
  position: relative;
  background-image: url('~@/assets/img/login-1.jpg');
  background-size: 100% 100%;
  .demo-ruleForm {
    position: relative;
    width: 450px;
    max-width: 100%;
    padding: 50px 35px 50px;
    margin: 200px auto;
    overflow: hidden;
    background-color: rgba(255, 255, 255, 0.2);
    border-radius: 10px;
  }
  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: #fff;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .el-button {
    height: 44px;
    border: 1px solid rgb(54, 181, 185);
    background-color: rgb(54, 181, 185);
  }
  .icon-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }
}
</style>
